package com.mzy.basics.utils.Io;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author mzy
 * @Date 2022/4/18 9:37
 * @Version 1.0
 */
public class DownBrowser {
    /**
     * 根据文件所在路径下载文件到浏览器
     *
     * @param request
     * @param response
     * @param filePath
     */

    public void download(HttpServletRequest request, HttpServletResponse response, String filePath) {

        File file = new File(filePath);

        // 取得文件名。

        String fileName = file.getName();

        InputStream fis = null;

        try {

            fis = new FileInputStream(file);

            request.setCharacterEncoding("UTF-8");

            String agent = request.getHeader("User-Agent").toUpperCase();

            if ((agent.indexOf("MSIE") > 0) || ((agent.indexOf("RV") != -1) && (agent.indexOf("FIREFOX") == -1)))

                fileName = URLEncoder.encode(fileName, "UTF-8");

            else {

                fileName = new String(fileName.getBytes("UTF-8"), "ISO8859-1");

            }

            response.reset();

            response.setCharacterEncoding("UTF-8");

            response.setContentType("application/force-download");// 设置强制下载不打开

            response.addHeader("Content-Disposition", "attachment;filename=" + fileName);

            response.setHeader("Content-Length", String.valueOf(file.length()));

            byte[] b = new byte[1024];

            int len;

            while ((len = fis.read(b)) != -1) {

                response.getOutputStream().write(b, 0, len);

            }

            response.flushBuffer();

            fis.close();

        } catch (IOException e) {

            throw new RuntimeException(e);

        }

    }
}
